<?php

/**
 * tcp服务
 */
//创建Server对象，监听 127.0.0.1:9501 端口
$server = new Swoole\Server('0.0.0.0', 9501);

$server->set(
    [
        'daemonize'=>true,
        'reactor_num'=>2,
        'worker_num'=>4
    ]
);
//监听连接进入事件 连接时输出
$server->on('Connect', function ($server, $fd) {
    echo "Client: Connect.\n";
});

//监听数据接收事件 返回内容
$server->on('Receive', function ($server, $fd, $reactor_id, $data) {
    var_dump($reactor_id);
    $server->send($fd, "Server: {$data}");
    $server->close($fd);
});

//监听连接关闭事件
$server->on('Close', function ($server, $fd) {
    echo "Client: Close.\n";
});

//启动服务器
$server->start();


